using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace TrafficWindowsService.Common
{
    public class OlivingLogTxt
    {


        private static object xflag = new object();
        private static FileStream createFile(string filename)
        {

            return new FileStream(filename, FileMode.Append);
        }
        public static void writeLog(string path, string filename, string msgstr)
        {

            lock (xflag)
            {
                if (!System.IO.Directory.Exists(path))
                {
                    //return;
                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {
                        try
                        {
                            path = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                            if (!System.IO.Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                        }
                        catch
                        {
                        }
                    }

                }
                string filepath = path + filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream fs = createFile(filepath);

                byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  :" + msgstr + "\r\n");
                fs.Write(data, 0, data.Length); fs.Flush();
                fs.Close();
            }
        }



        public static void writeLog(string filename, string msgstr)
        {
            lock (xflag)
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                if (!System.IO.Directory.Exists(path))
                {
                    //return;
                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {
                        try
                        {
                            path = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                            if (!System.IO.Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                        }
                        catch
                        {
                        }
                    }

                }

                string filepath = path + filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream fs = createFile(filepath);

                byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  :" + msgstr + "\r\n");
                fs.Write(data, 0, data.Length); fs.Flush();
                fs.Close();
            }
        }

        public static void writeException(string msgstr)
        {
            string filename = "Exception";
            lock (xflag)
            {
                string path = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                if (!System.IO.Directory.Exists(path))
                {
                    //return;
                    try
                    {
                        Directory.CreateDirectory(path);
                    }
                    catch
                    {
                        try
                        {
                            path = System.AppDomain.CurrentDomain.BaseDirectory + @"Log\";
                            if (!System.IO.Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                            }
                        }
                        catch (Exception e)
                        {
                        }
                    }

                }

                string filepath = path + filename + "_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream fs = createFile(filepath);

                byte[] data = System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  :" + msgstr + "\r\n");
                fs.Write(data, 0, data.Length); fs.Flush();
                fs.Close();
            }
        }





    }
}
